const express=require('express')
const router= express.Router();
const db=require('../../utils/db')


//渲染表格
router.get('/xuanran',function(req,res){
  let mysql=`SELECT * FROM menu_file`
  db(mysql,null,function(err,data){
    if(err){
        return res.json({
            code: 500,
            msg: '查询菜品失败，请稍后重试'
        })
    }
    res.json({
        code: 200,
        msg: '查询菜品成功',
        data: data
    })
})
})
//删除
router.post('/shuanchu',function(req,res){
    console.log(req.body);
    let str
    if(req.body.numb==1){
       str=req.body.data
    }else{
        str=req.body.data.toString()
    }
    let mysql=`  DELETE FROM menu_file WHERE menu_id in (${str})`
    db(mysql,null,function(err,data){
      if(err){
          return res.json({
              code: 500,
              msg: '删除失败，请稍后重试'
          })
      }
      res.json({
          code: 200,
          msg: '删除成功'
      })
  })
  })


  //编辑
router.post('/bianji',function(req,res){
    console.log(req.body);
    let mysql=`UPDATE menu_file SET menu_id =${req.body.menu_id},menu_name='${req.body.menu_name}',menu_price=${parseInt(req.body.menu_price)},menu_category='${req.body.menu_category}',menu_taste =${req.body.menu_taste},menu_size ='${req.body.menu_size}',menu_danwei ='${req.body.menu_danwei}',menu_status ='${req.body.menu_status}' WHERE menu_id =${req.body.menu_id}`
    db(mysql,null,function(err,data){
      if(err){
          return res.json({
              code: 500,
              msg: '修改失败，请稍后重试'
          })
      }
      res.json({
          code: 200,
          msg: '修改成功'
      })
  })
  })

  //增加
  router.post('/xinzen',function(req,res){
    console.log(req.body);
     let mysql=`INSERT INTO menu_file (menu_id,menu_price,menu_tj,menu_category,menu_taste,menu_name,menu_danwei,menu_size,menu_status,menu_amount,menu_note) VALUES (${parseInt(req.body.menu_num)},${parseInt(req.body.menu_price)},${parseInt(req.body.menu_tj)},'${req.body.menu_category}','${req.body.menu_taste}','${req.body.menu_name}','${req.body.menu_danwei}','${req.body.menu_size}','${req.body.menu_status}',${parseInt(req.body.menu_amount)},'${req.body.menu_note}');`
   
    db(mysql,null,function(err,data){
      if(err){
          return res.json({
              code: 500,
              msg: '增加失败，请稍后重试'
          })
      }
      res.json({
          code: 200,
          msg: '增加成功'
      })
  })
  })
module.exports = router